package com.xinlu.util.tool;

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.Tag;

/**
 * 
 * @author ocean
 * 
 */
public class PageSet implements Tag {

	// 分页关键字
	private String nameType = "条记录";

	// 分页对象
	private String key = "pageObject";

	// 在执行分页的过程，调用的方法名称。
	private String methodName = "loadPage";

	// 页面配置
	PageContext pageContext = null;

	public String getMethodName() {
		return methodName;
	}

	public void setMethodName(String methodName) {
		this.methodName = methodName;
	}

	public PageContext getPageContext() {
		return pageContext;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public String getNameType() {
		return nameType;
	}

	public void setNameType(String nameType) {
		this.nameType = nameType;
	}

	public int doEndTag() throws JspException {
		return this.EVAL_PAGE;
	}

	public int doStartTag() throws JspException {

		// String nameType_1 = getNameType();
		StringBuffer sb = new StringBuffer();
		PageObject pageObject = null;
		if (pageContext.getRequest().getAttribute(getKey()) != null) {
			pageObject = (PageObject) pageContext.getRequest().getAttribute(getKey());
		} else if (pageContext.getSession().getAttribute(getKey()) != null) {
			pageObject = (PageObject) pageContext.getSession().getAttribute(getKey());
		} else if (pageContext.getAttribute(getKey()) != null) {
			pageObject = (PageObject) pageContext.getAttribute(getKey());
		} else {
			pageObject = new PageObject();
		}
		pageObject.setItems(null); // 让jvm及时回收不必要的垃圾数据
		if (pageObject != null) {
			if (pageObject.getPageCount() > 0) {
				doPageSet4Computer(pageObject, sb);
			} else {
				sb.append("首页 | 上页 | 下页 | 末页");
			}

		}

		JspWriter out = null;

		out = pageContext.getOut();

		try {

			out.write(sb.toString());

		} catch (IOException e) {
			e.printStackTrace();
		}
		return this.SKIP_BODY;
	}

	public Tag getParent() {
		return null;
	}

	public void release() {

	}

	public void setPageContext(PageContext arg0) {
		this.pageContext = arg0;

	}

	public void setParent(Tag arg0) {

	}

	private void doPageSet4Computer(PageObject pageObject, StringBuffer sb) {

		if (((pageObject.getCurrentPageNo() - 2) > 0) && ((pageObject.getCurrentPageNo() + 2) < pageObject.getPageCount())) {
			for (int i = pageObject.getCurrentPageNo() - 2; i <= pageObject.getCurrentPageNo() + 2; i++) {
				if (pageObject.getCurrentPageNo() != i) {
					sb.append("<a href='#' onclick=\"javascript:setPage(" + i + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
					sb.append("[" + i + "]");
					sb.append("</a> ");
				} else {
					sb.append(i + " ");
				}
			}
			sb.append(" ... ");
			if (pageObject.getCurrentPageNo() != pageObject.getPageCount()) {
				sb.append("<a href='#' onclick=\"javascript:setPage(" + pageObject.getPageCount() + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
				sb.append("[" + pageObject.getPageCount() + "]");
				sb.append("</a> ");
			} else {
				sb.append(pageObject.getPageCount());
			}
		} else if (((pageObject.getCurrentPageNo() - 2) > 0) && ((pageObject.getCurrentPageNo() + 2) >= pageObject.getPageCount())) {
			for (int j = pageObject.getCurrentPageNo() - 2; j <= pageObject.getPageCount(); j++) {
				if (pageObject.getCurrentPageNo() != j) {
					sb.append("<a href='#' onclick=\"javascript:setPage(" + j + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
					sb.append("[" + j + "]");
					sb.append("</a> ");
				} else {
					sb.append(j + " ");
				}
			}
		} else {
			if (pageObject.getCurrentPageNo() + 2 < pageObject.getPageCount()) {
				for (int k = 1; k <= pageObject.getCurrentPageNo() + 2; k++) {
					if (pageObject.getCurrentPageNo() != k) {
						sb.append("<a href='#' onclick=\"javascript:setPage(" + k + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
						sb.append("[" + k + "]");
						sb.append("</a> ");
					} else {
						sb.append(k + " ");
					}
				}
				sb.append(" ... ");
				if (pageObject.getCurrentPageNo() != pageObject.getPageCount()) {
					sb.append("<a href='#' onclick=\"javascript:setPage(" + pageObject.getPageCount() + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
					sb.append("[" + pageObject.getPageCount() + "]");
					sb.append("</a> ");
				} else {
					sb.append(pageObject.getPageCount());
				}
			} else {
				for (int m = 1; m <= pageObject.getPageCount(); m++) {
					if (pageObject.getCurrentPageNo() != m) {
						sb.append("<a href='#' onclick=\"javascript:setPage(" + m + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
						sb.append("[" + m + "]");
						sb.append("</a> ");
					} else {
						sb.append(m + " ");
					}
				}
			}
		}
		sb.append("&nbsp;&nbsp;");
		if (pageObject.getCurrentPageNo() != 1) {
			sb.append("<a href='#' onclick=\"javascript:setPage(" + pageObject.getFirstPage() + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
			sb.append("首页");
			sb.append("</a>");
		} else {
			sb.append("首页");
		}
		sb.append("&nbsp;|&nbsp;");

		int frontPage = pageObject.getCurrentPageNo() - 1;
		int nextPage = pageObject.getCurrentPageNo() + 1;
		if (pageObject.getPreviousPage()) {
			sb.append("<a href='#' onclick=\"javascript:setPage(" + frontPage + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
			sb.append("上页");
			sb.append("</a>");
			sb.append("&nbsp;|&nbsp;");
		} else {
			sb.append("上页");
			sb.append("&nbsp;|&nbsp;");
		}
		if (pageObject.getNextPage()) {
			sb.append("<a href='#' onclick=\"javascript:setPage(" + nextPage + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
			sb.append("下页");
			sb.append("</a>&nbsp;|&nbsp;");
		} else {
			sb.append("下页");
			sb.append("&nbsp;|&nbsp;");
		}
		if (pageObject.getCurrentPageNo() != pageObject.getPageCount()) {
			sb.append("<a href='#' onclick=\"javascript:setPage(" + pageObject.getEndPage() + "); return submitCommonForm(document.forms[0],'"+getMethodName()+"')\">");
			sb.append("末页");
			sb.append("</a>");
		} else {
			sb.append("末页");
		}
	}
}